package com.offcn;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-mail.xml")
public class sendmessagetest1 {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Test
    public void test1(){
//            1,创建一个简单邮件发送对象
        SimpleMailMessage mailMessage = new SimpleMailMessage();

//            设置邮件属性
        mailMessage.setFrom("lmtssioi@126.com");
//        设置接收方
        mailMessage.setTo("lmtssioi@126.com");
//            设置邮件标题
        mailMessage.setSubject("java test sendEMail 001");
//            设置邮件正文
        mailMessage.setText("hello ，i am you ，how are you，you are not fine? never mind,because i am unhapply too ");
//            调用邮件发送类，发送邮件
        javaMailSender.send(mailMessage);
        System.out.println("邮件发送成功");
    }

    @Test
    public void test2(){
            //创建一个支持附件的邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            //创建一个助手类，帮助处理邮件属性         ,右面跟true表示支持文件附件
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
                //使用助手类，设置邮件属性
            helper.setFrom("lmtssioi@126.com");
            helper.setTo("lmtssioi@126.com");
            helper.setSubject("test mail002");
//            正文后跟参数true，支持html
            helper.setText("java test 002");

            File file = new File("A:\\img\\zmsy.png");
                //添加文件到付件
            helper.addAttachment("a.png",file);

            javaMailSender.send(mimeMessage);
            System.out.println("发送成功");

        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void test3(){
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom("lmtssioi@126.com");
            helper.setTo("lmtssioi@126.com");
            helper.setSubject("test mail003");
            System.out.println("sdfasdfwerdsgdsfg");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
